package com.bistu.xiong.tulingrobotchat.adapter;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.bistu.xiong.common.widget.PortraitView;
import com.bistu.xiong.common.widget.recycler.RecyclerAdapter;
import com.bistu.xiong.factory.bean.ChatMessage;
import com.bistu.xiong.factory.bean.DataWithTag;
import com.bistu.xiong.factory.persistence.Account;
import com.bistu.xiong.tulingrobotchat.R;
import com.bumptech.glide.Glide;
import com.github.library.bubbleview.BubbleTextVew;

import org.w3c.dom.Text;

import java.util.List;

/**
 * Created by XiongXiaoJun on 2017/12/25.
 */

public class ChatMessageRecAdapter extends RecyclerAdapter<ChatMessage> {

    public ChatMessageRecAdapter(List<ChatMessage> mDataList, Context context) {
        super(mDataList, (AdapterListener<ChatMessage>)context, context);
    }

    public ChatMessageRecAdapter(List<ChatMessage> mDataList, AdapterListener<ChatMessage> mListener, Context context) {
        super(mDataList, mListener, context);
    }

    @Override
    protected int getItemViewType(int position, ChatMessage chatMessage) {
        //假如是机器人的回复
        if (chatMessage.isRobot()){
            return R.layout.item_conversation_left;
        }
        return R.layout.item_conversation_right;
    }

    @Override
    protected ViewHolder<ChatMessage> onCreateViewHolder(View root, int viewType) {
        return new ChatViewHolder(context,root);
    }

    private class ChatViewHolder extends RecyclerAdapter.ViewHolder<ChatMessage>{
        private PortraitView portraitView;
        private BubbleTextVew btv_message;
        private TextView tv_time;
        private Context context;
        public ChatViewHolder(Context context,View itemView) {
            super(itemView);
            this.context = context;
            portraitView = itemView.findViewById(R.id.portraitView);
            btv_message = itemView.findViewById(R.id.btv_message);
            tv_time = itemView.findViewById(R.id.tv_time);
        }

        @Override
        protected void onBind(ChatMessage chatMessage) {
            if (!chatMessage.isRobot()){
                // 假如是用户发送消息 加载头像
                Glide.with(context)
                        .load(Account.getCurrentUser().getPortrait())
                        .asBitmap()
                        .centerCrop()
                        .placeholder(R.drawable.default_portrait) //默认显示头像
                        .into(portraitView);
            }else{
                //假如机器人的话
                Glide.with(context)
                        .load(R.drawable.tuling_robot_portrait)
                        .asBitmap()
                        .centerCrop()
                        .into(portraitView);
            }
            btv_message.setText(chatMessage.getText());
        }
    }
}
